package com.zcp.stickyheadersrecyclerview;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersAdapter;
import com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersDecoration;
import com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersTouchListener;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity {

    @BindView(R.id.rv)
    RecyclerView rv;
    private AnimalsHeadersAdapter adapter;
    private StickyRecyclerHeadersDecoration headersDecor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        initView();
        initEvent();
    }

    private void initView() {
        // 为RecyclerView设置适配器
        adapter = new AnimalsHeadersAdapter();
        adapter.addAll(getDummyDataSet());
        rv.setAdapter(adapter);
        // 为RecyclerView添加LayoutManager
        final LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        rv.setLayoutManager(layoutManager);
        // 为RecyclerView添加Decorator装饰器
        // 为RecyclerView中的Item添加Header头部（自动获取头部ID，将相邻的ID相同的聚合到一起形成一个Header）
        headersDecor = new StickyRecyclerHeadersDecoration(adapter);
        rv.addItemDecoration(headersDecor);
        // 为RecyclerView添加Item之间的分隔线
        rv.addItemDecoration(new DividerDecoration(this));
    }

    private void initEvent() {
        // 为RecyclerView添加普通Item的点击事件（点击Header无效）
        rv.addOnItemTouchListener(new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(MainActivity.this, adapter.getItem(position) + " Clicked", Toast.LENGTH_SHORT).show();
            }
        }));
        // 为RecyclerView添加Header的点击事件
        StickyRecyclerHeadersTouchListener touchListener = new StickyRecyclerHeadersTouchListener(rv, headersDecor);
        touchListener.setOnHeaderClickListener(new StickyRecyclerHeadersTouchListener.OnHeaderClickListener() {
            @Override
            public void onHeaderClick(View header, int position, final long headerId) {
                Toast.makeText(MainActivity.this, "Header position: " + position + ", id: " + headerId, Toast.LENGTH_SHORT).show();
            }
        });
        rv.addOnItemTouchListener(touchListener);
    }

    // 获取RecyclerView中展示的数据源
    private String[] getDummyDataSet() {
        return getResources().getStringArray(R.array.animals);
    }

    // StickyHeadersRecyclerView的适配器类
    private class AnimalsHeadersAdapter extends AnimalAdapter<RecyclerView.ViewHolder> implements StickyRecyclerHeadersAdapter<RecyclerView.ViewHolder> {
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listitem_normal, parent, false);
            return new RecyclerView.ViewHolder(view) {
            };
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            TextView textView = (TextView) holder.itemView;
            textView.setText(getItem(position));
        }

        // 获取当前Item的首字母，按照首字母将相邻的Item聚集起来并添加统一的头部
        @Override
        public long getHeaderId(int position) {
            return getItem(position).charAt(0);
        }

        // 获取头部布局
        @Override
        public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listitem_header, parent, false);
            return new RecyclerView.ViewHolder(view) {
            };
        }

        // 为头部布局中的控件绑定数据
        @Override
        public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {
            final TextView textView = (TextView) holder.itemView;
            textView.setText(String.valueOf(getItem(position).charAt(0)));
        }
    }
}
